package com.hanliy.dto;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;

/**
 * @author hanyulin
 * @apiNote 统一结果集
 * @date 2021/10/28 14:33
 */
@Data
@ApiModel(value = "统一消息结果dto")
public class Result<T> implements Serializable {

    @ApiModelProperty(value = "代码")
    private Integer code;
    @ApiModelProperty(value = "消息")
    private String msg;
    @ApiModelProperty(value = "数据")
    private T data;
    @ApiModelProperty(value = "运行时间")
    private Long runTime;

    public Result ok() {
        this.msg = ResponseEnum.SUCCESS.getMsg();
        this.code = ResponseEnum.SUCCESS.getCode();
        return this;
    }

    public Result ok(String msg) {
        this.msg = msg;
        this.code = ResponseEnum.SUCCESS.getCode();
        return this;
    }

    public Result ok(T data) {
        this.msg = ResponseEnum.SUCCESS.getMsg();
        this.code = ResponseEnum.SUCCESS.getCode();
        this.data = data;
        return this;
    }

    public Result fail() {
        this.msg = ResponseEnum.ERROR.getMsg();
        this.code = ResponseEnum.ERROR.getCode();
        return this;
    }

    public Result fail(String msg) {
        this.code = ResponseEnum.ERROR.getCode();
        this.msg = msg;
        return this;
    }

    public Result fail(ResponseEnum response) {
        this.msg = response.getMsg();
        this.code = response.getCode();
        return this;
    }

}
